Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\rust\core.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::compiler::Protocol;
30
use crate::gen::base::Error;
31
use crate::gen::codec::CodecMap;
32
use crate::gen::file::{Content, File, FileType};
33
use crate::gen::rust::message::{
34
    gen_message_decl, gen_message_from_slice_impl, gen_message_offsets_decl, gen_message_write_impl,
35
};
36
use crate::gen::rust::params::Params;
37
use crate::gen::rust::r#enum::gen_enum_decl;
38
use crate::gen::rust::structure::gen_structure_decl;
39
use crate::gen::rust::union::gen_union_decl;
40
use crate::gen::Generator;
41
use bp3d_debug::trace;
42
use std::path::Path;
43
use crate::codec_map_initializer;
44
use crate::gen::template::Template;
45
46
const TEMPLATE_CODEC_BASE: &[u8] = include_bytes!("./default_codec/base.template");
47
const TEMPLATE_CODEC_STRING: &[u8] = include_bytes!("./default_codec/string.template");
48
const TEMPLATE_CODEC_LIST: &[u8] = include_bytes!("./default_codec/list.template");
49
50
pub struct GeneratorRust;
51
52
impl Generator for GeneratorRust {
53
    type Error = Error;
54
    type Params<'a> = Params<'a>;
55
56
2
    fn get_default_codecs<'fragment, 'variable>() -> CodecMap<'fragment, 'variable> {
57
2
        let mut codecs = CodecMap::new(codec_map_initializer! {
58
2
            "base" => TEMPLATE_CODEC_BASE
59
2
        });
60
2
        codecs.insert("string", Template::compile_with_includes(TEMPLATE_CODEC_STRING, &codecs).unwrap());
61
2
        codecs.insert("list", Template::compile_with_includes(TEMPLATE_CODEC_LIST, &codecs).unwrap());
62
2
        codecs
63
2
    }
64
65
35
    fn generate(proto: &Protocol, codec_map: &CodecMap, params: &Params) -> Result<Vec<File>, Self::Error> {
66
35
        trace!({?params}, "Generating protocol {}", proto.full_name);
67
35
        let decl_messages_code =
68
35
            proto.messages.iter().map(|v| 
gen_message_decl(v, codec_map, &proto.type_path_map, params)28
);
69
35
        let impl_from_slice_messages_code =
70
35
            proto.messages.iter().map(|v| 
gen_message_from_slice_impl(v, codec_map, &proto.type_path_map)28
);
71
35
        let impl_write_messages_code =
72
35
            proto.messages.iter().map(|v| 
gen_message_write_impl(v, codec_map, &proto.type_path_map, params)28
);
73
59
        let decl_structures = proto.structs.iter().map(|v| gen_structure_decl(v, &proto.type_path_map, params));
74
35
        let decl_enums = proto.enums.iter().map(|v| 
gen_enum_decl(v)6
);
75
35
        let decl_unions = proto.unions.iter().map(|v| 
gen_union_decl(v, &proto.type_path_map, params)6
);
76
35
        let mut files = vec![
77
35
            File::new(
78
35
                FileType::Message,
79
35
                "messages.rs",
80
35
                &Content::try_from_iter(decl_messages_code)
?0
,
81
            ),
82
            File::new(
83
35
                FileType::MessageReading,
84
35
                "messages_from_bytes.rs",
85
35
                &Content::try_from_iter(impl_from_slice_messages_code)
?0
,
86
            ),
87
            File::new(
88
35
                FileType::MessageWriting,
89
35
                "messages_write.rs",
90
35
                &Content::try_from_iter(impl_write_messages_code)
?0
,
91
            ),
92
35
            File::new(
93
35
                FileType::Structure,
94
35
                "structures.rs",
95
35
                &Content::from_iter(decl_structures),
96
35
            ),
97
35
            File::new(FileType::Enum, "enums.rs", &Content::from_iter(decl_enums)),
98
35
            File::new(FileType::Union, "unions.rs", &Content::from_iter(decl_unions)),
99
35
        ];
100
35
        if params.enable_message_offsets {
  Branch (100:12): [True: 1, False: 34]
  Branch (100:12): [Folded - Ignored]
101
1
            let decl_messages_code_offsets =
102
2
                proto.messages.iter().map(|v| gen_message_offsets_decl(v, codec_map, &proto.type_path_map));
103
1
            files.push(File::new(
104
1
                FileType::MessageReading,
105
1
                "messages_offsets.rs",
106
1
                &Content::try_from_iter(decl_messages_code_offsets)
?0
,
107
            ));
108
34
        }
109
35
        Ok(files)
110
35
    }
111
112
35
    fn generate_umbrella<'a>(
113
35
        proto_name: &str,
114
35
        files: impl Iterator<Item = &'a Path>,
115
35
        _: &Params,
116
35
    ) -> Result<String, Self::Error> {
117
35
        let mut code = format!("pub mod {} {{\n", proto_name);
118
124
        for 
file89
in files {
119
89
            code += &format!("include!({:?});\n", file);
120
89
        }
121
35
        code += "}\n";
122
35
        Ok(code)
123
35
    }
124
125
244
    fn get_language_extension() -> &'static str {
126
244
        "rs"
127
244
    }
128
}